package org.train.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.train.dao.ProjectDao;
import org.train.dao.RevenueExpenditureDao;
import org.train.entity.ProjectPo;
import org.train.entity.RevenueExpenditureInfoVo;
import org.train.entity.RevenueExpenditurePo;
import org.train.util.AmountUtil;
import org.train.util.R;
import org.train.util.RUtil;
import org.train.util.Renum;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class RevenueExpenditureService extends ServiceImpl<RevenueExpenditureDao, RevenueExpenditurePo>{
    @Autowired
    private ProjectDao pd;
    @Autowired
    private RevenueExpenditureDao red;

    public R queryAllInfos(Integer pageNum, Integer pageSize, String search){
        List<RevenueExpenditureInfoVo> result = new ArrayList<>();

        List<ProjectPo> projectPoList;
        if(search != null){
            Page<ProjectPo> page = pd.selectPage(new Page<ProjectPo>(pageNum, pageSize, false),
                    new QueryWrapper<ProjectPo>()
                            .eq("deleted", 0)
                            .and(queryWrapper->{
                                queryWrapper.like("contract_name", search).or().like("project_year", search);
                            }));
            projectPoList = page.getRecords();
        }else{
            Page<ProjectPo> page = pd.selectPage(new Page<ProjectPo>(pageNum, pageSize, false),
                    new QueryWrapper<ProjectPo>()
                            .eq("deleted", 0));
            projectPoList = page.getRecords();
        }
        if(projectPoList.size() == 0){
            // 对不不存在满足要求的项目的情况，直接返回空数组，不进行后续的计算
            return RUtil.success(result);
        }

        for(ProjectPo projectPo: projectPoList){
            RevenueExpenditureInfoVo one = new RevenueExpenditureInfoVo().assignProjectPo(projectPo);
            List<RevenueExpenditurePo> reps = red.selectList(new QueryWrapper<RevenueExpenditurePo>()
                    .eq("project_id", one.getId())
                    .eq("deleted", 0));

            // 当前年使用当前年份
            int currYear = Calendar.getInstance().getWeekYear();
            AmountUtil.Amounts amounts = AmountUtil.calcAmounts(reps, currYear);
            one.setTotalBilling(amounts.totalBilling);
            one.setAnnualBilling(amounts.annualBilling);
            one.setTotalFundsReceived(amounts.totalFundsReceived);
            one.setAnnualFundsReceived(amounts.annualFundsReceived);
            one.setAnnualExpenditure(amounts.annualExpenditure);

            result.add(one);
        }
        return RUtil.success(result);
    }

    public R saveOrUpdateOne(RevenueExpenditurePo po){
        // 根据产生日期，设置年度
        po.setProjectYear(po.getGenerateTime().getYear() + 1900);
        super.saveOrUpdate(po);
        return RUtil.success();
    }
}
